﻿using System;
using System.Collections.Generic;
using CkSoftware.GroupMe.Sdk.Core.Serialization;
using Newtonsoft.Json;

namespace CkSoftware.GroupMe.Sdk.Core.Dao
{
	/// <summary>
	/// GroupMe message DAO containing all message-relevant data fields from the GroupMe API for a single message.
	/// </summary>
	public class GroupMeMessageData
	{
		/// <summary>
		/// The list of all attachments to this message.
		/// </summary>
		[JsonProperty(PropertyName = "attachments")]
		[JsonConverter(typeof (GroupMeAttachmentConverter))]
		public IEnumerable<GroupMeAttachmentBase> Attachments { get; set; }

		/// <summary>
		/// Parsed <see cref="PictureData" /> object from the AvatarUrl.
		/// </summary>
		[JsonIgnore]
		public PictureData Avatar
		{
			get { return new PictureData {Url = AvatarUrl}; }
		}

		/// <summary>
		/// The avatar url of the message-poster member.
		/// </summary>
		[JsonProperty(PropertyName = "avatar_url")]
		public string AvatarUrl { get; set; }

		/// <summary>
		/// The parsed CreatedAt DateTime.
		/// </summary>
		[JsonIgnore]
		public DateTime CreatedAt
		{
			get { return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(CreatedSecondsSinceEpoch); }
		}

		/// <summary>
		/// The CreatedAt DateTime in unix notation (seconds since epoch).
		/// </summary>
		[JsonProperty(PropertyName = "created_at")]
		public long CreatedSecondsSinceEpoch { get; set; }

		/// <summary>
		/// The id of the group where this message was posted.
		/// </summary>
		[JsonProperty(PropertyName = "group_id")]
		public string GroupId { get; set; }

		/// <summary>
		/// Indicates if this is a system-generated message (eg. a user leave / add or a nickname change).
		/// </summary>
		[JsonProperty("system")]
		public bool IsSystemMessage { get; set; }

		/// <summary>
		/// Contains all user-id's which liked this message.
		/// </summary>
		[JsonProperty(PropertyName = "favorited_by")]
		public IEnumerable<string> LikedByUserIds { get; set; }

		/// <summary>
		/// The unique message id.
		/// </summary>
		[JsonProperty(PropertyName = "id")]
		public string MessageId { get; set; }

		/// <summary>
		/// The name of the message-poster member.
		/// </summary>
		[JsonProperty(PropertyName = "name")]
		public string Name { get; set; }

		/// <summary>
		/// The raw Json-data of this message (can be used if a custom parsing is needed).
		/// </summary>
		[JsonIgnore]
		public string RawData { get; set; }

		/// <summary>
		/// The DateTime when this message was received from this framework (will only be filled out on a bot callback. In every
		/// other case this is always DateTime.MinValue!).
		/// </summary>
		[JsonIgnore]
		public DateTime Received { get; set; }

		/// <summary>
		/// A guid to identify a message, can be used for client-side deduplication.
		/// </summary>
		[JsonProperty(PropertyName = "source_guid")]
		public string SourceGuid { get; set; }

		/// <summary>
		/// The text of this message.
		/// </summary>
		[JsonProperty(PropertyName = "text")]
		public string Text { get; set; }

		/// <summary>
		/// The UserId of the message-poster member.
		/// </summary>
		[JsonProperty(PropertyName = "user_id")]
		public string UserId { get; set; }
	}
}